#include "SendFile.h"
#include "ui_SendFile.h"
#include "WindowManager.h"
#include "TalkWindowShell.h"
#include <QFileDialog>
#include <QFile>

SendFile::SendFile(QWidget *parent)
	: BasicWindow(parent),
	m_filePath("")
{
	ui = new Ui::SendFile();
	ui->setupUi(this);

	initTitleBar();
	setTitleBarTitle("发送文件", ":/Resources/MainWindow/qqlogoclassic.png");
	loadStyleSheet("SendFile");

	TalkWindowShell *shell = WindowManager::getInstance()->m_talkwindowshell;
	connect(
		this, SIGNAL(sendFileClicked(QString &, int, QString)),
			shell, SLOT(updateSendTcpMsg(QString &, int, QString))
		);


	//setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);

}

SendFile::~SendFile()
{
	delete ui;
}

void SendFile::on_sendBtn_clicked()
{
	if (!m_filePath.isEmpty())
	{
		QFile file(m_filePath);
		if (file.open(QIODevice::ReadOnly))
		{	
			QFileInfo info(m_filePath);
			QString fileName = info.fileName();
			int msgType = 2;
			QString strData = file.readAll();

			emit sendFileClicked(strData, msgType, fileName);

			file.close();
		}

		ui->lineEdit->clear();
		m_filePath = "";
		close();
	}
}

void SendFile::on_openBtn_clicked()
{
	m_filePath = QFileDialog::getOpenFileName(
		this,
		"选择文件",
		"/",
		"发送的文件(*.TXT *.DOX);;所有文件(*.*);;"
	);

	ui->lineEdit->setText(m_filePath);
}
